package lan.battletank.logic;

import lan.battletank.element.BaseElement;
import lan.battletank.element.PlayerBulletEntity;
import lan.battletank.element.ControlPanelEntity;
import lan.battletank.element.IndicatorEntity;
import lan.battletank.element.TankEntity;

public class ElementStatusLogic {
	
	/**
	 * Determine all elements' current status.
	 * @param tank
	 * @param bullet
	 * @param controlPanel
	 * @param indicator
	 */
	public static void setStatus(TankEntity tank, PlayerBulletEntity bullet, ControlPanelEntity controlPanel, IndicatorEntity indicator) {
        tank.isMoving = (controlPanel.goLeft() || controlPanel.goRight());
        tank.isAdjusting = (controlPanel.angleUp() || controlPanel.angleDown());
        if (!tank.isFiring()) {
		    if (controlPanel.firePressed()) {
		    	tank.setPreparingToFire(true);
		    	indicator.setChangingFirePower(true);
		    } else if (tank.isPreparingToFire()) {
				tank.startFiring(bullet);
		    	bullet.setBulletmomentum(tank.getCannonAngle(), indicator.getFirePower());
		    	tank.setPreparingToFire(false);
		    	indicator.setChangingFirePower(false);
		    }
        }
    }
	
	/**
	 * Determine if the two element is colliding.
	 * @param e1
	 * @param e2
	 * @return
	 */
	public static boolean isColliding(BaseElement e1, BaseElement e2) {
		if ((e1.centerX + e1.halfWidth) > (e2.centerX - e2.halfWidth)
				&& (e1.centerX - e1.halfWidth) < (e2.centerX + e2.halfWidth)
				&& (e1.centerY + e1.halfHeight) > (e2.centerY - e2.halfHeight)
				&& (e1.centerY - e1.halfHeight) < (e2.centerY + e2.halfHeight)) {
			return true;
		}
		return false;
	}
	
	/**
	 * Determine if the e1's center is reaching the e2's surface.
	 * @param e1
	 * @param e2
	 * @return
	 */
	public static boolean isCenterColliding(BaseElement e1, BaseElement e2) {
		if (e1.centerX > (e2.centerX - e2.halfWidth)
				&& e1.centerX < (e2.centerX + e2.halfWidth)
				&& e1.centerY > (e2.centerY - e2.halfHeight)
				&& e1.centerY < (e2.centerY + e2.halfHeight)) {
			return true;
		}
		return false;
	}
	
}
